package com.caeit.controllers.bigscreen;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.caeit.aop.log.ALog;
import com.caeit.exception.BadRequestException;
import com.caeit.model.bigscreen.BigscreenConfig;
import com.caeit.service.bigscreen.BigscreenConfigService;
import com.caeit.service.bigscreen.dto.BigscreenConfigQuery;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 业务应用表;(bigscreen_config)表控制层
 * @author : chenyr
 * @date : 2022-8-22
 */
@RestController
@RequestMapping("/api/bigscreenConfig")
public class BigscreenConfigController {

    private final BigscreenConfigService bigscreenConfigService;

    public BigscreenConfigController(BigscreenConfigService bigscreenConfigService) {
        this.bigscreenConfigService = bigscreenConfigService;
    }

    /**
     * 分页查询
     *
     * @param query 筛选条件
     * @param page 分页对象
     * @return 查询结果
     */
    @GetMapping("/paginQuery")
    @ALog("业务应用列表")
//    @PreAuthorize("hasAnyRole('admin','child:list')")
    public ResponseEntity<Object> paginQuery(BigscreenConfigQuery query, @PageableDefault(sort = {"id"}, direction = Sort.Direction.ASC) Pageable page) {
        return new ResponseEntity<>(bigscreenConfigService.paginQuery(query, page), HttpStatus.OK);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @GetMapping("/queryById/{id}")
    public ResponseEntity<Object> queryById(@PathVariable(name = "id") Long id){
        return new ResponseEntity<>(bigscreenConfigService.queryById(id), HttpStatus.OK);
    }

    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     */
    @GetMapping("/queryEnableData")
    public ResponseEntity<Object> queryEnableData(){
        return new ResponseEntity<>(bigscreenConfigService.queryEnableData(), HttpStatus.OK);
    }

    /**
     * 新增数据
     *
     * @param resources 实例对象
     * @return 实例对象
     */
    @PostMapping("/save")
    public ResponseEntity<Object> save(@Validated @RequestBody BigscreenConfig resources) {
        resources.setId(null);
        bigscreenConfigService.save(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 更新数据
     *
     * @param resources 实例对象
     * @return 实例对象
     */
    @PostMapping("/edit")
    public ResponseEntity<Object> edit(@Validated @RequestBody BigscreenConfig resources) {
        if (resources.getId() == null) {
            throw new BadRequestException("ID不能为空！");
        }
        bigscreenConfigService.edit(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Object> delete(@PathVariable Long id) {
        bigscreenConfigService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
